import { ref } from 'vue'
import { defineStore } from 'pinia'
import { apiAddDiagnosis } from '@/api/Service/api'

export const userConsuliation = defineStore('consuliation', () => {
  //当前步骤
  const current = ref(0)

  //步骤条数据
  const items = ref([
    {
      title: '诊断',
    },
    {
      title: '开药',
    },
    {
      title: '就诊建议',
    },
  ])

  // 诊断信息 也就是最后要提交的数据
  const diagnosisInfo = ref({
    symptom: '',
    descriptions: '',
    isfever: true,
    isEpidemic: false,
    isComplication: false,
    degree: null,
    drugInfo: [],
    totalPrice: 0,
  })

  //点击诊断下一步
  const oneNext = (data) => {
    current.value++
    diagnosisInfo.value = { ...diagnosisInfo.value, ...data }
  }

  //点击药品的添加
  const addDrugInfo = (list, totalPrice) => {
    diagnosisInfo.value.totalPrice = totalPrice
    list.forEach((element) => {
      let { drugsId, drugNumber, frequency } = element
      diagnosisInfo.value.drugInfo.push({
        drugsId,
        drugNumber,
        frequency,
      })
    })
    current.value++
  }

  //就诊建议的添加
  const addSuggestion = (suggestion) => {
    diagnosisInfo.value.suggestion = suggestion
  }

  //添加就诊记录
  const addRecord = async () => {
    current.value = 4
    return await apiAddDiagnosis(diagnosisInfo.value)
  }

  //将状态回归到初始
  const backTreatment = () => {
    // 清空数据
    Object.keys(diagnosisInfo.value).forEach(
      (key) => delete diagnosisInfo.value[key]
    )
    diagnosisInfo.value.drugInfo = []
    current.value = 0
  }

  const getCurrent = () => {
    return current.value
  }

  return {
    current,
    items,
    oneNext,
    addDrugInfo,
    addRecord,
    addSuggestion,
    backTreatment,
    getCurrent,
  }
})
